/**
 * 集群模块控制器
 * 
 * @author zhongfuhua
 */

var jobGroupApp = angular.module("jobGroupApp");

jobGroupApp.controller("jobGroupRunningController", [
		"$scope",
		"commonService",
		"jobDetailService",
		"jobDetailTrackService",
		"$timeout",
		"$log",
		function($scope, commonService, jobDetailService, jobDetailTrackService, $timeout, $log) {
			
			
			// 表格
			$scope.$jobGroupRunningTable = $('#jobGroupRunningTable');
			
			$scope.$jobGroupInterruptButton = $('#jobGroupInterruptButton');
			$scope.$jobGroupResetButton = $('#jobGroupResetButton');
			$scope.$jobGroupFinishButton = $('#jobGroupFinishButton');
			
			$scope.$jobGroupRunningModal = $('#jobGroupRunningModal');
			$scope.$jobGroupRunningThreadModal = $('#jobGroupRunningThreadModal');
			
			$scope.jobGroupRetryTrackTableCurrent = null;
			
			// 表格选中实例对象
			$scope.selectionIds = new Array();
			$scope.selections = new Array();
			
			// 监视isSucce的状态
			$scope.action = null;
			$scope.isSuccess = true;
			$scope.error = null;
			
			// vo
			$scope.jobDetailThreadInfoVO = {
				jobProcessThread : null,
				jobProcessBusinessThread : null
			};
			
			$scope.jobDetailRunningVO = {
				jobGroupName : null,
				clusterName : null
			};
			
			
			$scope.runStatusSelectConfig = {
				minimumResultsForSearch : 1, // 是否打开搜索
				data : [{text:"已接收",id:"RECIVE"},
                        {text:"离线",id:"OFFLINE"},
                        {text:"离线超时",id:"OFFLINE_TIMEOUT"},
                        {text:"超时",id:"TIMEOUT"}
                       ],			 
				placeholder : '请选择'			 // 占位符
			};
			
			ffc.util.watchResult($scope, $timeout);
			
			
			$scope.publicVO.jobDetailRunningInit = function(row){
				$scope.jobDetailRunningVO = {};
				$scope.jobDetailRunningVO.jobGroupName = row.name;
				$scope.jobDetailRunningVO.clusterName = row.clusterName;
				ffc.util.angularApply($scope, $timeout);
				$scope.queryJobDetailRunning();
				$scope.$jobGroupRunningModal.modal("show");
			}
			
			/**
			 * 
			 * 功能说明:查询运行中任务
			 * 
			 */
			$scope.queryJobDetailRunning = function(){
				$scope.$jobGroupRunningTable.bootstrapTable("selectPage", 1);  
			}
			
			
			/**
			 * 
			 * 功能说明:查询任务组下拉框数据
			 * 
			 */
			$scope.queryJobDetailThreadInfo = function(){
				jobDetailService.queryThreadInfo($scope.jobDetailRunningVO, function(result){
					if(!result.result){
						$scope.error = result.msgTitle;
						$scope.isSuccess = false;
						return false;
					}
					$scope.jobDetailThreadInfoVO.jobProcessThread = result.object.jobProcessThread;
					$scope.jobDetailThreadInfoVO.jobProcessBusinessThread = result.object.jobProcessBusinessThread;
					ffc.util.angularApply($scope, $timeout);
				},function(error){
					$scope.error = error;
					$scope.isSuccess = false;
				});
			}
			
			
			
			/**
			 * 
			 * 功能说明:唤醒子任务
			 * 
			 */
			$scope.interruptJobDetail = function(){
				new MessageDialog().confirm("提示：离线子任务不可中断，确定中断子任务？", function() {
					jobDetailService.interruptList($scope.selections, function(result){
						if(!result.result){
							$scope.error = result.msgTitle;
							$scope.isSuccess = false;
							return false;
						}
						new MessageDialog().alert("中断成功!!");
						$scope.queryJobDetailRunning();
					},function(error){
						$scope.error = error;
						$scope.is
					});
			   });
			}
			
			
			
			
			/**
			 * 
			 * 功能说明:唤醒子任务
			 * 
			 */
			$scope.resetJobDetail = function(){
				new MessageDialog().confirm("确定重置子任务？", function() {
					jobDetailService.resetList($scope.selections, function(result){
						if(!result.result){
							$scope.error = result.msgTitle;
							$scope.isSuccess = false;
							return false;
						}
						new MessageDialog().alert("重置成功!!");
						$scope.queryJobDetailRunning();
					},function(error){
						$scope.error = error;
						$scope.is
					});
			   });
			}
			
			
			
			/**
			 * 
			 * 功能说明:唤醒子任务
			 * 
			 */
			$scope.finishJobDetail = function(){
				new MessageDialog().confirm("确定结束子任务？", function() {
					jobDetailService.finishList($scope.selections, function(result){
						if(!result.result){
							$scope.error = result.msgTitle;
							$scope.isSuccess = false;
							return false;
						}
						new MessageDialog().alert("结束成功!!");
						$scope.queryJobDetailRunning();
					},function(error){
						$scope.error = error;
						$scope.is
					});
			   });
			}
			
			
			$(function() {
				
				/**
				 * 
				 * 功能说明:初始化数据源表格
				 * 
				 */
				$scope.$jobGroupRunningTable.bootstrapTable({
					queryParams : function(params) {
						var selfParams = {
							jobGroupName : $scope.jobDetailRunningVO.jobGroupName,
							clusterName : $scope.jobDetailRunningVO.clusterName,
							status : $scope.jobDetailRunningVO.queryStatus == "" ? null : $scope.jobDetailRunningVO.queryStatus,
							addr : $scope.jobDetailRunningVO.queryAddr,
							id : $scope.jobDetailRunningVO.queryId
						}
						return $.extend({}, params, selfParams);
					},
					ajax : function(render) {
						jobDetailService.queryRunningPage(render.data, function(result) {
							if(!result.result){
								$scope.error = result.msgTitle;
								$scope.isSuccess = false;
								return false;
							}
							ffc.util.loadTableData(render, result);  
						},function(error){
							$scope.error = error;
							$scope.isSuccess = false;
						});
					},
					onExpandRow : function(index, row, $detail){
						var jobGroupRetryTrackTable = $detail.html(
								"<div class=\"table-responsive\">" +
									"<table name='jobGroupRetryTrackTable'></table>" +
								"</div>"
								).find("table");
						
						var columns = [];
						columns.push({
							field : "opaque",
							title : "opaque",
							width : "5%",
							align : "center",
							classes : "table-column",
							valign : "middle"
						});
						
						columns.push({
							field : "addr",
							title : "地址",
							width : "35%",
							align : "center",
							classes : "table-column",
							valign : "middle"
						});
						
						columns.push({
							field : "fireTime",
							title : "触发时间",
							width : "15%",
							align : "center",
							classes : "table-column",
							valign : "middle"
						});
						
						columns.push({
							field : "sendTime",
							title : "发送时间",
							width : "15%",
							align : "center",
							classes : "table-column",
							valign : "middle"
						});
						
						columns.push({
							field : "reciveTime",
							title : "执行端接收时间",
							width : "15%",
							align : "center",
							classes : "table-column",
							valign : "middle"
						});
						
						columns.push({
							field : "completeTime",
							title : "完成时间",
							width : "15%",
							align : "center",
							classes : "table-column",
							valign : "middle"
						});
						
						$scope.jobGroupRetryTrackTableCurrent = jobGroupRetryTrackTable;
						
						jobGroupRetryTrackTable.bootstrapTable({
							idField : "id",
							sidePagination: "server",
							columns : columns,
							pagination : true,
							pageList : [5, 10, 20],
							detailView : false,
							queryParamsType : "",
							queryParams : function(params) {
								var selfParams = {
									id : row.id,
									jobName : row.jobName,
									clusterName : row.clusterName
								};
								return $.extend({}, params, selfParams);
							},
							ajax : function(render) {
								jobDetailTrackService.queryPage(render.data, function(result) {
									if(!result.result){
										$scope.error = result.msgTitle;
										$scope.isSuccess = false;
										return false;
									}
									ffc.util.loadTableData(render, result);  
								},function(error){
									$scope.error = error;
									$scope.isSuccess = false;
								});
							}
						});
					}
				});
				
				
				
				/**
				 * 
				 * 功能说明:每次选择时重新获取表格中被选中对象
				 * 
				 */
				$scope.$jobGroupRunningTable.on('check.bs.table uncheck.bs.table check-all.bs.table uncheck-all.bs.table',function () {
					$scope.$jobGroupInterruptButton.prop('disabled', !$scope.$jobGroupRunningTable.bootstrapTable('getSelections').length);
					$scope.$jobGroupResetButton.prop('disabled', !$scope.$jobGroupRunningTable.bootstrapTable('getSelections').length);
					$scope.$jobGroupFinishButton.prop('disabled', !$scope.$jobGroupRunningTable.bootstrapTable('getSelections').length);
					$scope.selectionIds = new Array();
					$scope.selections = new Array();
					angular.forEach($scope.$jobGroupRunningTable.bootstrapTable('getSelections'), function(data, index, array) {
						$scope.selectionIds.push(data.id);
						$scope.selections.push(data);
					});
				})
				
				
				/**
				 * 
				 * 功能说明:每次选择时重新获取表格中被选中对象
				 * 
				 */
				$scope.$jobGroupRunningTable.on('load-success.bs.table', function (data) {
					$('[data-toggle="popover"]').popover();
				});
				
				
				
				
				/**
				 * 
				 * 功能说明:弹出框关闭事件
				 * 
				 */
				$scope.$jobGroupRunningModal.on("hidden.bs.modal",function(){
					$scope.jobDetailRunningVO = {};
				});
				
				
				/**
				 * 
				 * 功能说明:弹出框关闭事件
				 * 
				 */
				$scope.$jobGroupRunningThreadModal.on("hidden.bs.modal",function(){
					$scope.jobDetailThreadInfoVO = {
						jobProcessThread : null,
						jobProcessBusinessThread : null
					};
				});
				
				
				
				/**
				 * 
				 * 功能说明:格式化表格输出
				 * 
				 */
				jobDetailRunningOperFormatter = function(value, row, index) {
					var button = "<button type=\"button\" class=\"btn btn-xs btn-blue\" style=\"margin-right: 3px;\">线程</button>";
					return button;
				}
				
				
				/**
				 * 
				 * 功能说明:格式化表格输出
				 * 
				 */
				statusFormatter = function(value, row, index) {
					if(value == 'FIRE'){
						return "开始";
					}else if(value == 'SEND'){
						return "已发送"
					}else if(value == 'RECIVE'){
						return "送达"
					}else if(value == 'COMPLETE'){
						return "完成"
					}else if(value == 'OFFLINE'){
						return "离线"
					}else if(value == 'TIMEOUT'){
						return "超时"
					}else if(value == 'OFFLINE_TIMEOUT'){
						return "离线超时"
					}
					return value;
				}
				
				
				/**
				 * 
				 * 功能说明:格式化事件
				 * 
				 */
				jobDetailRunningOperEvents = {
					'click .btn-blue' : function(e, value, row, index) {
						$scope.jobDetailRunningVO = {};
						$scope.jobDetailRunningVO.id = row.id;
						$scope.jobDetailRunningVO.jobDetailName = row.jobDetailName;
						$scope.jobDetailRunningVO.jobName = row.jobName;
						$scope.jobDetailRunningVO.clusterName = row.clusterName;
						ffc.util.angularApply($scope, $timeout);
						$scope.queryJobDetailThreadInfo();
						$scope.$jobGroupRunningThreadModal.modal("show");
					}
				}
			});
}]);
